/**
 * Description: UploadUtil.java
 * Copyright (c) 2008-2009  Neo. 
 * All Rights Reserved.
 * @version 1.0  Apr 12, 2009 11:20:35 AM 李萌（neolimeng@gmail.com）created
 */
package org.paradise.dms.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;

/**
 * Description: Copyright (c) 2008-2009 Neo. All Rights Reserved.
 * 
 * @version 1.0 Apr 12, 2009 11:20:35 AM 李萌（neolimeng@gmail.com）created
 */
public class UploadUtil {
	private static Logger log = Logger.getLogger(UploadUtil.class);
	private static FileOutputStream fos;
	private static FileInputStream fis;

	/**
	 * 
	 * Description:静态方法：将输入文件保存到指定位置。
	 * 
	 * @Version1.0 Apr 12, 2009 11:28:02 AM 李萌（neolimeng@gmail.com）创建
	 * @param uploadedFile
	 * @param savePath
	 * @return
	 */
	public static boolean uploadFile(File uploadedFile, String savePath) {
		try {
			fos = new FileOutputStream(savePath);
			log.info("UploadUtil保存文件到：" + savePath);
			fis = new FileInputStream(uploadedFile);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			log.error("UploadUtil上传文件成功！！");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("UploadUtil上传文件失败！！");
			return false;
		} finally {
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}

	/**
	 * 
	 * Description:静态方法：取得指定文件的后缀。
	 * 
	 * @Version1.0 Apr 12, 2009 11:36:17 AM 李萌（neolimeng@gmail.com）创建
	 * @param fileName
	 * @return
	 */
	public static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}
}
